<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="mallName" label="商场名称">
          <el-input v-model="queryParams.mallName" placeholder="请输入商场名称" clearable />
        </el-form-item>
        <el-form-item prop="mallName" label="电话">
          <el-input v-model="queryParams.phonenumber" placeholder="请输入电话" clearable />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="search" @click="search">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-card flex-1">
      <div class="button-box flex-x-between mb-10px">
        <div>
          <el-button type="success" icon="plus" @click="handleOpenDialog">新增</el-button>
        </div>
      </div>
      <div class="table-card-body">
        <BasePageTable :table-data="tableData" :table-columns-data="tableColumnsData" :page-num="queryParams.pageNum"
          :page-size="queryParams.pageSize" :total="total" max-height="600" :loading="loading"
          @handle-query="handleChangePage">
          <template #operation="{ row, name }">
            <el-button link type="primary" icon="Edit" @click="handleOpenDialog(row)">
              编辑
            </el-button>
            <el-button link type="danger" icon="Delete" @click="handleDelete(row.id)">
              删除
            </el-button>
          </template>
        </BasePageTable>
      </div>
    </el-card>
    <sys-shopping-mall-form ref="sysShoppingMallRef" @load-data="handleQuery"></sys-shopping-mall-form>
  </div>
</template>

<script setup lang="ts">
import BasePageTable from "@/components/BasePageTable/index.vue";
import mallApi, { MallInfoVO, MallPageQuery } from "@/api/mall/mall-api";
import SysShoppingMallForm from "@/views/system/sys-shopping-mall/components/sys-shopping-mall-form.vue";

const loading = ref(false);
const sysShoppingMallRef = ref();
const total = ref(0);
const queryFormRef = ref();

const queryParams = reactive<MallPageQuery>({
  mallName: "",
  phonenumber: "",
  pageNum: 1,
  pageSize: 10,
});
const tableData = ref<MallInfoVO[]>([]);
const tableColumnsData = ref([
  {
    prop: "mallName",
    label: "合伙人名称",
    showOverflowTooltip: true,
  },
  {
    prop: "address",
    label: "地址",
    showOverflowTooltip: true,
  },
  {
    prop: "mallCode",
    label: "编码",
    showOverflowTooltip: true,
  },

  {
    prop: "phonenumber",
    label: "电话",
    showOverflowTooltip: true,
  },
  {
    prop: "remark",
    label: "备注",
    showOverflowTooltip: true,
  },
  {
    prop: "operation",
    label: "操作",
    width: 150,
    align: "center",
    fixed: "right",
    customSlot: "operation", // 自定义插槽名称
  },
]);

function handleChangePage({ pageNum, pageSize }) {
  queryParams.pageNum = pageNum;
  queryParams.pageSize = pageSize;
  handleQuery();
}

function search() {
  queryParams.pageNum = 1;
  handleQuery();
}
// 查询
function handleQuery() {
  loading.value = true;
  mallApi
    .getPage(queryParams)
    .then((data: any) => {
      tableData.value = data.data || [];
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

async function handleDelete(id) {
  const res = await ElMessageBox.confirm("确认删除吗?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  });

  if (!res) return;
  loading.value = true;
  try {
    await mallApi.delete(id);
    handleQuery();
    ElMessage.success("删除成功");
  } catch (e) {
    // ElMessage.warning("删除失败");
  } finally {
    loading.value = false;
  }
}

function handleOpenDialog(row) {
  sysShoppingMallRef.value.showModal(row);
}

onMounted(async () => {
  handleQuery();
});
</script>
<style scoped lang="scss">
.table-card {
  :deep(.el-card__body) {
    height: 100%;
    display: flex;
    flex-direction: column;
  }

  .table-card-body {
    height: calc(100% - 50px);
  }

  .button-box {
    height: 50px;
  }
}
</style>
